const Service = require("egg").Service;
const { Builder, By, Key, until } = require("selenium-webdriver");
class ManyteleService extends Service {
  constructor(ctx) {
    super(ctx);
  }

  async getCode(body) {
    let { mobile, ua, productCode } = body;
    let url =
      "https://third.qtsay.com/right/index?code=2232507211966gos242336443";
    if (!mobile || !ua || !productCode) {
      // 可以自定义状态码
      this.ctx.status = 500;
      this.ctx.msg = "缺少必要参数";
      return;
    }

    if (!/^1[3456789]\d{9}$/.test(mobile)) {
      this.ctx.status = 500;
      this.ctx.msg = "请填写正确手机号";
      return;
    }
    let userId = mobile + "_" + productCode;
    // 为用户获取窗口
    let driver = await this.service.seleniumPool.getUserWindow(userId);
    if (!driver) {
      let window = await this.service.seleniumPool.acquire(userId);

      if (!window) {
        this.ctx.status = 500;
        this.ctx.msg = "活动太火爆了，请稍后再试";
        return;
      }
    }
    driver = await this.service.seleniumPool.getUserWindow(userId);
    driver = driver.driver;
    try {
      await driver.get(url);
      await driver.wait(until.elementLocated(By.className("relative")), 1000);
      let phoneList = await driver.findElements(By.className("relative"));
      phoneList = phoneList[1];
      let phone = await phoneList.findElements(By.tagName("input"));
      let phoneNumber = mobile;
      phoneNumber = phoneNumber.split(".")[0];
      await phone[0].sendKeys(phoneNumber);

      //同意阅读
      let agreeList = await driver.findElements(
        By.className("van-badge__wrapper van-icon van-icon-circle")
      );
      await agreeList[0].click();
      //提交
      let submitList = await driver.findElements(By.className("dom_loading"));

      await submitList[0].click();

      // await driver.sleep(3000);
      let check = false;
      let time = new Date().valueOf();
      let checkTime = new Date().valueOf();
      let checkStamp = 3000;
      let iFrame = "";
      while (!check && checkTime - time < checkStamp) {
        try {
          iFrame = await driver.findElements(By.tagName("iframe"));
          iFrame = await iFrame[0].getAttribute("src");
          if (iFrame && iFrame != url) {
            check = true;
          }
        } catch (error) {}
        checkTime = new Date().valueOf();
      }
      if (iFrame == url) {
        this.ctx.status = 500;
        this.ctx.msg = "发送验证码失败，您的手机号暂不支持办理";
        return;
      }
      await driver.get(iFrame);
      let order_no = "";
      try {
        order_no = iFrame.split("order_no=")[1];
        order_no = order_no.split("&")[0];
      } catch (error) {}
      this.ctx.status = 200;
      this.ctx.msg = "发送成功！";
      this.ctx.body = {
        orderNumber: order_no,
        url: iFrame,
      };
    } catch (error) {
      console.log(error);
      this.ctx.status = 500;
      this.ctx.msg = "发送验证码失败，请稍后再试一次吧";
      this.ctx.body = {
        error: error.message,
      };
    }

    try {
      await driver.wait(
        until.elementLocated(By.className("verify_text_p")),
        1000
      );
      //点击获取验证码
      let hasCode = await driver.findElements(By.className("verify_text_p"));
      await hasCode[0].click();
    } catch (error) {}
  }

  async submit(body) {
    let { mobile, ua, productCode, vCode } = body;

    if (!mobile || !ua || !productCode || !vCode) {
      // 可以自定义状态码
      this.ctx.status = 500;
      this.ctx.body = {
        message: "缺少必要参数",
      };
    }

    if (!/^1[3456789]\d{9}$/.test(mobile)) {
      this.ctx.status = 500;
      this.ctx.msg = "请填写正确手机号";
      return;
    }

    let userId = mobile + "_" + productCode;
    // 为用户获取窗口
    let driver = await this.service.seleniumPool.getUserWindow(userId);
    if (!driver) {
      this.ctx.status = 500;
      this.ctx.msg = "请先获取验证码";
      return;
    }
    driver = driver.driver;
    try {
      //填写验证码
      let verifCodeList = await driver.findElements(
        By.className("verify_code_p")
      );
      //   let vCodeList = await verifCodeList[0].findElements(By.tagName("input"));
      //   await vCodeList[0].sendKeys(vCode);
      await verifCodeList[0].sendKeys(vCode);
      await driver.sleep(1000);

      //提交
      let sureDom = await driver.findElements(By.className("confirm_p"));
      await sureDom[0].click();
      await driver.sleep(1000);
      await driver.findElements(By.className("mint-toast-text"), 10000);

      let text = await driver.findElements(By.className("mint-toast-text"));
      text = await text[0].getText();
      this.ctx.status = 200;
      this.ctx.body = {
        message: text || "提交成功！",
      };
    } catch (error) {
      console.log(error);
      this.ctx.status = 500;
      this.ctx.msg = "办理失败";
      this.ctx.body = {
        error: error,
      };
    }
  }
}

module.exports = ManyteleService;
